package com.igeekhome.microserviceconsumermovie8001.config;

import com.igeekhome.microservice.feign.UserFeignService;
import com.igeekhome.microservice.feign.UserFeignServiceFallBack;
import com.igeekhome.microservice.feign.UserFeignServiceFallBackFactory;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author yadonghe
 * @date 2020-02-21 10:49
 */
@Configuration
public class MyWebConfig {

    //@LoadBalanced:让RestTemplate具有负载均衡的功能
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }



    //配置全局的负载均衡算法
    //不推荐
    /*
    @Bean
    public IRule randomRule() {
        return new RandomRule();
    }
    */

    //指定服务配置负载均衡算法
    //注意：自定义算法一定不能放在springboot扫描的目录下，否则该算法会成为全局算法


    @Bean
    public UserFeignServiceFallBack userFeignServiceFallBack() {
        return new UserFeignServiceFallBack();
    }

    @Bean
    public UserFeignServiceFallBackFactory userFeignServiceFallBackFactory() {
        return new UserFeignServiceFallBackFactory();
    }
}
